home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROCS.ZIP / LONGSTR.ICN < prev    next >
Text File  |  1992-12-31  |  2KB  |  83 lines

  1. ############################################################################
  2. #
  3. #    File:     longstr.icn
  4. #
  5. #    Subject:  Procedure to match longest string
  6. #
  7. #    Authors:  Jerry Nowlin, Steve Wampler, Kenneth Walker, Bob
  8. #                 Alexander, and Richard Goerwitz
  9. #
  10. #    Date:     June 1, 1991
  11. #
  12. ###########################################################################
  13. #
  14. #    Version:  1.9
  15. #
  16. ###########################################################################
  17. #
  18. #  longstr(l,s,i,j) works like any(), except that instead of taking a
  19. #  cset as its first argument, it takes instead a list or set of
  20. #  strings (l).  Returns i + *x, where x is the longest string in l
  21. #  for which match(x,s,i,j) succeeds.  Fails if no match occurs.
  22. #
  23. #  Defaults:
  24. #      s     &subject
  25. #      i     &pos if s is defaulted, otherwise 1
  26. #      j     0
  27. #
  28. #  Errors:
  29. #      The only manual error-checking that is done is to test l to
  30. #      be sure it is, in fact, a list or set.  Errors such as non-
  31. #      string members in l, and non-integer i/j parameters, are
  32. #      caught by the normal Icon built-in string processing and sub-
  33. #      scripting mechanisms.
  34. #
  35. ############################################################################
  36.  
  37. procedure longstr(l,s,i,j)
  38.  
  39.     local elem, tmp_table
  40.     static l_table
  41.     initial l_table := table()
  42.  
  43.     #
  44.     # No-arg invocation wipes out all static structures, and forces an
  45.     # immediate garbage collection.
  46.     #
  47.     if (/l, /s) then {
  48.     l_table := table()
  49.     collect()        # do it NOW
  50.     return            # return &null
  51.     }
  52.  
  53.     #
  54.     # Is l a list, set, or table?
  55.     #
  56.     type(l) == ("list"|"set"|"table") |
  57.     stop("longstr:  list, set, or table expected (arg 1)")
  58.  
  59.     #
  60.     # Sort l longest-to-shortest, and keep a copy of the resulting
  61.     # structure in l_table[l] for later use.
  62.     #
  63.     if /l_table[l] := [] then {
  64.  
  65.     tmp_table := table()
  66.     # keys = lengths of elements, values = elements
  67.     every elem := !l do {
  68.         /tmp_table[*elem] := []
  69.         put(tmp_table[*elem], elem)
  70.     }
  71.     # sort by key; stuff values, in reverse order, into a list
  72.     every put(l_table[l], !sort(tmp_table,3)[*tmp_table*2 to 2 by -2])
  73.  
  74.     }
  75.  
  76.     #
  77.     # First element in l_table[l] to match is the longest match (it's
  78.     # sorted longest-to-shortest, remember?).
  79.     #
  80.     return match(!l_table[l],s,i,j)
  81.  
  82. end
  83.